<template>
  <div>
    <el-dialog
      :visible.sync="visible"
      :title="title"
      width="45%"
      :before-close="handleClose"
    >
      <!-- 表单 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="100px"
      >
        <el-input v-model="addForm.id" v-show="false"></el-input>
        <el-form-item label="配置标题" prop="info">
          <el-input v-model="addForm.info" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="配置键名" prop="code">
          <el-input v-model="addForm.code" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="配置类型" prop="type">
          <el-select
            v-model="addForm.type"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              key="1"
              label="字符串"
              :value="typeof addForm.type == 'string' ? '1' : 1"
            ></el-option>
            <el-option
              key="2"
              label="数字"
              :value="typeof addForm.type == 'string' ? '2' : 2"
            ></el-option>
            <el-option
              key="3"
              label="数组"
              :value="typeof addForm.type == 'string' ? '3' : 3"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配置值" prop="value">
          <el-input v-model="addForm.value" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button class="theme-background-color" @click="saveConfigData"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    paramConfigAddDialogVisible: {
      type: Boolean,
      default: function () {
        return {}
      }
    },
    formData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data() {
    return {
      addForm: {
        id: '',
        info: '',
        code: '',
        type: '',
        value: ''
      },
      addFormRules: {
        info: [
          { required: true, message: '请输入', trigger: 'blur' },
          {
            min: 1,
            max: 200,
            message: '长度在 1 到 200 个字符',
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 1, max: 40, message: '长度在 1 到 40 个字符', trigger: 'blur' }
        ],
        type: [{ required: true, message: '请选择', trigger: 'change' }],
        value: [
          { required: true, message: '请输入', trigger: 'blur' },
          {
            min: 1,
            max: 200,
            message: '长度在 1 到 200 个字符',
            trigger: 'blur'
          }
        ]
      },
      visible: this.paramConfigAddDialogVisible,
      title: '新增参数配置'
    }
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          this.resetAllData()
          this.$emit('update:paramConfigAddDialogVisible', false)
          done()
        })
        .catch((_) => {})
    },
    resetAllData() {
      // 清空校验
      this.$refs.addFormRef.resetFields()
    },
    closeDialog() {
      this.resetAllData()
      this.$emit('update:paramConfigAddDialogVisible', false)
    },
    saveConfigData() {
      this.$refs.addFormRef.validate(async (valid) => {
        // console.log(valid)
        if (!valid) return

        let url = ''
        if (this.addForm.id) {
          url = '/admin/config/modi'
        } else {
          url = '/admin/config/add'
        }
        // 新增的请求
        const { data: res } = await this.$http.post(url, this.addForm)
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.$message.success(res.meta.msg)
        this.$parent.getListData()
        this.visible = false
        this.$emit('update:paramConfigAddDialogVisible', false)
        this.resetAllData()
      })
    }
  },
  watch: {
    paramConfigAddDialogVisible() {
      this.visible = this.paramConfigAddDialogVisible
      // console.log(this.formData)
      if (this.visible && this.formData.id) {
        this.title = '编辑参数配置'
        this.addForm = JSON.parse(JSON.stringify(this.formData))
      } else {
        this.title = '新增参数配置'
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 500px;
  .el-select {
    width: 100%;
  }
}
</style>
